<html>
	<head>
		<title>The Real Franker!</title>
		<script type="text/javascript" src="http://www.google.com/jsapi">
	    </script>
		<script type="text/javascript">
			google.load("language", "1");
		</script>
		<script type="text/javascript">
			// broadcasts a message to all pages, used to propagate the settings (see issue #7)
			function broadcastMessage(name, payload) {
				for (var w = 0; w < safari.application.browserWindows.length; w++) {
					for (var t = 0; t < safari.application.browserWindows[w].tabs.length; t++) {
						safari.application.browserWindows[w].tabs[t].page.dispatchMessage(name, payload);
					}
				}
			}
			// sends a message to active page
			function sendMessage(name, payload) {
				safari.application.activeBrowserWindow.activeTab.page.dispatchMessage(name, payload);
			}
		</script>
		<script type="text/javascript">
			function translateCallBack(result) {
				sendMessage("frankateSelectionResponse", result.translation);
			}
			function respondToMessage(theMessageEvent) {
				switch (theMessageEvent.name) {
					case "frankateSelectionRequest":
						var srcText = theMessageEvent.message;
						var langDst = safari.extension.settings.lang_dst;
						if (safari.extension.settings.lang_src == "auto") {
							google.language.translate(srcText, "", langDst, translateCallBack);
						} else {
							google.language.translate(srcText, safari.extension.settings.lang_src, langDst, translateCallBack);
						}
						break;
					
					// settings
					case "shortcutFrankateSelectionRequest":
						broadcastMessage("shortcutFrankateSelectionValue", safari.extension.settings.shortcut_frankate_selection);
						break;
					case "shortcutFrankateClearRequest":
						broadcastMessage("shortcutFrankateClearValue", safari.extension.settings.shortcut_frankate_clear);
						break;
					case "styleDestinationRequest":
						broadcastMessage("styleDestinationValue", safari.extension.settings.style_dst);
						break;

					// extra
					case "frankatePageRequest":
						var langSrc=safari.extension.settings.lang_src;
						if (langSrc == "auto") {
							langSrc = "" // means "detect language"
						}
						var langDst=safari.extension.settings.lang_dst;
						var activeTab = safari.application.activeBrowserWindow.activeTab;
						activeTab.url = 'http://translate.google.com/translate?u='+escape(activeTab.url)+'&hl='+langDst+'&ie=UTF-8&sl='+langSrc+'&tl='+langDst
						break;
					case "shortcutFrankatePageRequest":
						broadcastMessage("shortcutFrankatePageValue", safari.extension.settings.shortcut_frankate_page);
						break;
					case "statePageEnabledRequest":
						sendMessage("statePageEnabledValue", safari.extension.settings.state_frankate_page);
						break;
				}
			}
			safari.application.addEventListener("message", respondToMessage, false);
		</script>
		<!-- Context menu and toolbar buttons watcher -->
		<script type="text/javascript">
			function performCommand(event) {
				if (event.command === "frankate_selection") {
					sendMessage("frankateSelection", "");
				}
				if (event.command === "frankate_page") {
					sendMessage("frankatePage", "");
				}
			}
			safari.application.addEventListener("command", performCommand, false);
		</script>
		<!-- Settings change watcher -->
		<script>
			function settingsChanged(event) {
				switch (event.key) {
					case "shortcut_frankate_selection":
						broadcastMessage("shortcutFrankateSelectionValue", event.newValue + ":" + event.oldValue);
						break;
					case "shortcut_frankate_clear":
						broadcastMessage("shortcutFrankateClearValue", event.newValue + ":" + event.oldValue);
						break;
					case "shortcut_frankate_page":
						broadcastMessage("shortcutFrankatePageValue", event.newValue + ":" + event.oldValue);
						break;
				}
			}
			safari.extension.settings.addEventListener("change", settingsChanged, false);
		</script>
	</head>
	<body>
	</body>
</html>
